﻿@using Opserver.Data.SQL
@using Opserver.Views.Shared.Guages
@inject SQLModule Sql
@{
    var all = Sql.AllInstances;
}
<div class="panel panel-default">
    <div class="panel-heading">@all.GetWorstStatus().IconSpan() SQL</div>
    <div class="panel-body">
        <div class="col-xs-4">
            <partial Name="Gauges/Circle" Model="@(new CircleModel("Instance", all))" />
        </div>
        <div class="col-xs-4">
            <partial Name="Gauges/Circle" Model="@(new CircleModel("AG", Sql.Clusters.SelectMany(c => c.AvailabilityGroups)))" />
        </div>
        <div class="col-xs-4">
            <partial Name="Gauges/Circle" Model="@(new CircleModel("Database", all.SelectMany(i => i.Databases.SafeData(true))))" />
        </div>
        <table class="table table-super-condensed table-striped col-md-12">
            @foreach (var c in Sql.Clusters)
            {
                <tbody>
                <tr>
                    <th>@c.Name</th>
                    <th>CPU</th>
                    <th>Mem</th>
                    <th>DBs</th>
                    <th>Batches</th>
                </tr>
                @foreach (var i in c.Nodes)
                {
                    <tr class="@i.RowClass()@(i.IsAllAGsPrimary ? " info" : null)">
                        <td>@i.IconSpan() <a href="~/sql/instance?node=@i.Name.UrlEncode()">@i.Name</a></td>
                        <td><img src="~/graph/sql/cpu/spark?node=@i.Name&time=@DateTime.UtcNow.ToString("yyyy-MM-dd")" width="100" height="16"/></td>
                        <partial Name="Partials.MemoryCell" Model="new PartialsMemoryCellModel(i)" />
                        <td>@Health.Description(i.Databases.SafeData(true))</td>
                        <td>@(i.BatchesPerSec?.ToComma())</td>
                    </tr>
                }
                </tbody>
            }
            <tbody>
            <tr>
                <th>Standalone</th>
                <th>CPU</th>
                <th>Mem</th>
                <th>DBs</th>
                <th>Batches</th>
            </tr>
            @foreach (var i in Sql.StandaloneInstances)
            {
                <tr class="@i.RowClass()">
                    <td>@i.IconSpan() <a href="~/sql/instance?node=@i.Name.UrlEncode()">@i.Name</a></td>
                    <td><img src="~/graph/sql/cpu/spark?node=@i.Name&time=@DateTime.UtcNow.ToString("yyyy-MM-dd")" width="100" height="16"/></td>
                    <partial Name="Partials.MemoryCell" Model="new PartialsMemoryCellModel(i)" />
                    <td>@Health.Description(i.Databases.SafeData(true))</td>
                    <td>@(i.BatchesPerSec?.ToComma())</td>
                </tr>
            }
            </tbody>
            <tfoot>
                <tr class="total-row">
                    <td><strong>Total</strong></td>
                    <td></td>
                    <td></td>
                    <td>@all.Sum(i => i.Databases.Data?.Count ?? 0).ToComma()</td>
                    <td>@all.Sum(i => i.BatchesPerSec ?? 0).ToComma()</td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>